# Contributor: Pegah Bahramiani <pb.bahramiani@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=lua-lapis
_rockname=lapis
pkgver=1.16.0
pkgrel=1
pkgdesc="A web framework for Lua/MoonScript supporting OpenResty or http.server"
url="https://leafo.net/lapis/"
license="MIT"
arch="noarch !ppc64le" #blocked by lua-etlua, which requires luajit
depends="
	lua
	lua-ansicolors
	lua-cjson
	lua-date
	lua-etlua
	lua-filesystem
	lua-loadkit
	lua-lpeg
	lua-ossl
	lua-pgmoon
	lua-socket
	lua-argparse
	lua-lsqlite3
"
makedepends="lua-dev luarocks"
checkdepends="lua-busted lua-tableshape lua-posix"
source="lapis-$pkgver.tar.gz::https://github.com/leafo/lapis/archive/v$pkgver.tar.gz
	lapis-5.1
	lapis-5.2
	lapis-5.3
	lapis-5.4
	lapis5.1.initd
	lapis5.2.initd
	lapis5.3.initd
	lapis5.4.initd
	unpack.patch
	skip_nothing_test.patch"
builddir="$srcdir/$_rockname-$pkgver"
subpackages="$pkgname-doc"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev luarocks$_v"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage lua$_v-${pkgname#lua-}-openrc:_subopenrc"
	checkdepends="$checkdepends lua$_v-busted lua$_v-moonscript lua$_v-tableshape lua$_v-posix"
	depends="
		$depends
		lua$_v
		lua$_v-ansicolors
		lua$_v-cjson
		lua-date
		lua$_v-etlua
		lua$_v-filesystem
		lua$_v-loadkit
		lua$_v-lpeg
		lua$_v-ossl
		lua$_v-pgmoon
		lua$_v-socket
		lua-argparse
		lua$_v-lsqlite3
	"
done

prepare() {
	default_prepare
}

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		luarocks-$lver \
			LUA_INCDIR="$(pkg-config --variable=includedir lua$lver)" \
			LUA_LIBDIR="$(pkg-config --variable=libdir lua$lver)" \
			make --tree="./build" \
			--deps-mode=none \
			"$builddir/$_rockname-dev-1.rockspec"
		rm "./build/lib/luarocks/rocks-$lver/manifest"
	done
}

check() {
	mkdir .bin

	local lver; for lver in $_luaversions; do
		msg "Testing on lua$lver..."
		ln -sf "/usr/bin/lua$lver" ".bin/lua"
		LUA_PATH="$builddir/?.lua;;" \
		PATH="$builddir/.bin:$PATH" \
		lua "/usr/bin/busted"
	done
}

package() {
	mkdir -p "$pkgdir"
	mkdir -p "$pkgdir/usr/share/doc/$pkgname/"

	install -Dm644 "$builddir"/docs/*.md "$pkgdir/usr/share/doc/$pkgname/"
	install -Dm644 "$builddir"/LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver $depends"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"

	install -Dm 755 "$srcdir/$_rockname-$lver" "$subpkgdir/usr/bin/$_rockname$lver"
	local path; for path in "lib/luarocks/rocks-$lver" "share/lua/$lver"; do
		  mkdir -p "$subpkgdir/usr/${path%/*}"
		  mv "$builddir/build/$path" "$subpkgdir/usr/$path/"
	done
}

_subopenrc() {
	local lver="${subpkgname:3:3}"

	install -Dm755 "$srcdir"/lapis$lver.initd "$subpkgdir/etc/init.d/lapis$lver"
}

sha512sums="
6dc861dd62a070b0e83da8b0b53a80d228fc66df1a15dc767de7fd8511cb17408deaa78660aef809b9c9f4c889d215d870dd099ae620c2559d3da7f10bbe9bcd  lapis-1.16.0.tar.gz
c4be44e42089e9c99aff2c092a2654b4b7f3814cbc2036c2d0c43742302760be189bd00072584433c78bcee25bfd37a0ced0f62da0f0d27d04e52f3b64a4e33e  lapis-5.1
1fd1c693b07c6099bfb2fd47d940d078ebaadeaa1720521ed572f1a2d777cbbef4190bff950ac20f1acb84f883cbc2d7c15fc71ff2693d155750e9668e9d798e  lapis-5.2
18399c4193b3e0add31f436e2d6c801baa97cf62e4765ddc56959cc31ba4c0b04ad2e1f134fb98409fef5cc84868e08a0e93d9eaa3d66d4b613355644551f343  lapis-5.3
921bd0081b42843b330c10ab265dd727a30692b40dfd37c4aced187d5b85f67f29a56d06466d7755c399b96c8f9ad9491e946112d1a777ad5b17a4770d38065b  lapis-5.4
bff494b5b4e0ab4e9687153cdd6d943aa8e7ede0f8afeba145d8c428351ffe6edd4eb99bbe84781c9b414ea878c335d7e8c2fa135260c485f2a8320f7f278c51  lapis5.1.initd
fa6a6ad171609fd32163ad64afcbc8708d730ce192ea8640319f5e6729826a5eb7a1344f585a5ad8400fc0fd47303b0645137073da94e8de819997623a75a87c  lapis5.2.initd
b58fd4b8ecae60c3368cd797277226a78aaca48f61fa68726c0485cfcb0d920a51dbdad10b0b8e7d48b4dae76cacb5b65a2af829289b309a332a0868537418b5  lapis5.3.initd
67d7841676bc08916bf99ac969d47f0771a0a58dfc0e22bdfe5a13a2d4673be4308e7b9f77fb3a5282f492881d6d63ea74d189e92bdd877b7a4990fa61cdc178  lapis5.4.initd
1e9c9f9d9521cadc38056daf9e2e6f503d8f4f9a86c9d115ec2d7d7eaec4b6d14645d6a7d8682869af7e42b6a77a310e645bfbb514585d0899a723fe6857a15a  unpack.patch
9163a2f4b7a44b8d9f3548f12a89b4036b143bedb2dd980dc6db15aeb184b55456ad276422cb60c67531ce729dcc65dd8707d17a1fb4d51684f31a97a65f8e36  skip_nothing_test.patch
"
